package com.yztstudy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yztstudy.dto.Result;
import com.yztstudy.entity.Voucher;
import com.yztstudy.mapper.VoucherMapper;
import com.yztstudy.entity.SeckillVoucher;
import com.yztstudy.service.ISeckillVoucherService;
import com.yztstudy.service.IVoucherService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class VoucherServiceImpl extends ServiceImpl<VoucherMapper, Voucher> implements IVoucherService {

    @Resource
    private ISeckillVoucherService seckillVoucherService;

    /**
     * TODO 返回所有的优惠券
     * @param shopId
     */
    @Override
    public Result queryVoucherOfShop(Long shopId) {
        // TODO 1.查询优惠券信息
        List<Voucher> vouchers = getBaseMapper().queryVoucherOfShop(shopId);
        // TODO 2.返回结果
        return Result.ok(vouchers);
    }

    /**
     * TODO 添加秒杀券
     * @param voucher
     */
    @Override
    @Transactional
    public void addSeckillVoucher(Voucher voucher) {
        // TODO 1.保存优惠券
        save(voucher); //写第一张表

        // TODO 2.保存秒杀信息
        SeckillVoucher seckillVoucher = new SeckillVoucher(); //写第二张表
        seckillVoucher.setVoucherId(voucher.getId());
        seckillVoucher.setStock(voucher.getStock());
        seckillVoucher.setBeginTime(voucher.getBeginTime());
        seckillVoucher.setEndTime(voucher.getEndTime());
        seckillVoucherService.save(seckillVoucher);

    }
}
